#!/bin/bash
#pMusic - build gtkdialog xml code
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8

######################################################
#                                                    #
#                      F I L E                       #
#                                                    #
######################################################

S='
<menu label="'$(gettext 'File')'">
 <menuitem stock="gtk-cdrom" label="'$(gettext 'Play CD-audio')'">'
  [ "$CD_DEVICE" = none ] && S=$S'<visible>disabled</visible>'
  S=$S'<action>'$APPDIR'/func_cd -play_cd</action>
  <action>refresh:PLAYLIST</action>
 </menuitem>
 <menuitem stock="gtk-save-as" label="'$(gettext 'Export tracks')'..."><action>'$APPDIR'/func_export &</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem stock="gtk-preferences" label="'$(gettext 'Preferences')'..."><action>'$APPDIR'/func_config -preferences &</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem stock="gtk-cdrom" label="'$(gettext 'Eject CD')'">'
  [ "$CD_DEVICE" = none ] && S=$S'<visible>disabled</visible>'
  S=$S'<action>'$APPDIR'/func_player -stop -skip_update</action>
  <action>eject</action>
 </menuitem>
 <menuitem stock="gtk-quit" label="'$(gettext 'Quit')'" accel-key="0x071" accel-mods="4">
  <action>'$APPDIR'/func_config -write_config</action>
  <action>'$APPDIR'/func -quit</action>
  <action>exit:Exit</action>
 </menuitem>
 <menuitem stock="gtk-quit" label="'$(gettext 'Quit and save gui/geometry')'">
  <action>'$APPDIR'/func_config -write_config</action>
  <action>rm '$WORKDIR'/xml_pmusic*</action>
  <action>'$APPDIR'/func -quit</action>
  <action>exit:Exit</action>
 </menuitem>
</menu>'
GUI_MENU_FILE="$S"



######################################################
#                                                    #
#             M U S I C   S O U R C E S              #
#                                                    #
######################################################

#built top 100 menuitems for jamendo
TOP100_MENU=''
for I in ' ' Blues Classical Electro Jazz Hiphop Lounge Metal Pop Rock; do
  TOP100_MENU=$TOP100_MENU'<menuitem label="Top 100 '$I'">
   <action>'$APPDIR'/func_jamendo -jamendo_top100 '$I' &</action>
  </menuitem>'
done

 S='<menu label="'$(gettext 'Music sources')'">

###Local Files
 <menu label="'$(gettext 'Files')'">
  <menuitem label="'$(gettext 'Setup')'..."><action>'$APPDIR'/func_index -update_index use_meta &</action></menuitem>
  <menuitemseparator></menuitemseparator>
  <menuitem label="'$(gettext 'Browse')'">
   <action signal="button-press-event">clear:SOURCE</action>
   <action>export SOURCE="'$BROWSERDIR'"; . '$APPDIR'/func -browse</action>
   <action>refresh:SOURCE</action>
   <action>refresh:SEARCH</action>
  </menuitem>
  <menuitem label="'$(gettext 'Advanced file search')'..."><action>'$APPDIR'/func -pfilesearch</action></menuitem>
 </menu>

###Radio stations 
 <menu label="'$(gettext 'Radio stations')'">
  <menuitem label="'$(gettext 'Setup')'..."><action>. '$APPDIR'/func_radio &</action></menuitem>
  <menuitemseparator></menuitemseparator>
  <menuitem label="'$(gettext 'Browse genre')'">
   <action signal="button-press-event">clear:SOURCE</action>
   <action>'$APPDIR'/func -radio -genre</action>
   <action>refresh:SOURCE</action>
  </menuitem>
  <menuitem label="'$(gettext 'Browse country')'">
   <action signal="button-press-event">clear:SOURCE</action>
   <action>'$APPDIR'/func -radio -country</action>
   <action>refresh:SOURCE</action>
  </menuitem>
  <menuitemseparator></menuitemseparator>
  <menuitem label="'$(gettext 'Radio grabber')'..."><action>. '$APPDIR'/func_radiohits &</action></menuitem>
 </menu>

 <menu label="'$(gettext 'Podcast')'">
  <menuitem label="'$(gettext 'Setup')'..."><action>'$APPDIR'/func_podcast -setup &</action></menuitem>
  <menuitemseparator></menuitemseparator>
  <menuitem label="'$(gettext 'Browse')'">
   <action>'$APPDIR'/func_podcast -show_podcast</action>
   <action>refresh:SOURCE</action>
  </menuitem>
 </menu>

###Jamendo
 <menu label="Jamendo">
  <menuitem label="'$(gettext 'Setup')'..."><action>'$APPDIR'/func_jamendo -jamendo_manager &</action></menuitem>
  <menuitemseparator></menuitemseparator>
  '$TOP100_MENU'
  <menuitemseparator></menuitemseparator>
  <menuitem label="'$(gettext 'Browse')'">
   <action>'$APPDIR'/func_jamendo -jamendo</action>
   <action>refresh:SOURCE</action>
  </menuitem>
 </menu>

 <menuitem label="CD">'
  [ "$CD_DEVICE" = none ] && S=$S'<visible>disabled</visible>'
  S=$S'<action>echo "|'$(gettext 'Probing disc')'" > '$WORKDIR'/browser</action>
  <action>'$APPDIR'/func_cd -read_cd &</action>
  <action>refresh:SOURCE</action>
 </menuitem>

 <menuitemseparator></menuitemseparator>

 <menuitem stock="gtk-favorites" label="'$(gettext 'Favorites')'">
  <action signal="button-press-event">clear:SOURCE</action>
  <action>'$APPDIR'/func_favorites -show_favorites</action>
  <action>refresh:SOURCE</action>
 </menuitem>
 <menuitem label="'$(gettext 'Most played')'..."><action>'$APPDIR'/func_rating -custom</action></menuitem>
 <menuitem label="'$(gettext 'Played history')'">
  <action signal="button-press-event">clear:SOURCE</action>
  <action>'$APPDIR'/func -index_recent</action>
  <action>refresh:SOURCE</action>
 </menuitem>
 <menuitem label="'$(gettext 'Source history')'">
  <action signal="button-press-event">clear:SOURCE</action>
  <action>'$APPDIR'/func -index_history</action>
  <action>refresh:SOURCE</action>
 </menuitem>
</menu>'
GUI_MENU_SOURCES="$S"



######################################################
#                                                    #
#                 P L A Y Q U E U E                  #
#                                                    #
######################################################

S='
<menu label="'$(gettext 'PlayQueue')'">
 <menuitem stock="gtk-pmusic_clear" label="'$(gettext 'Clear')'" accel-key="0x06e" accel-mods="4">
  <action>'$APPDIR'/func_store_playlist &</action>
  <action>sleep 0.2; '$APPDIR'/func -new</action>
  <action>refresh:PLAYLIST</action>
 </menuitem>

 <menuitemseparator></menuitemseparator>

 <menuitem stock="gtk-save" label="'$(gettext 'Save playlist')'..." accel-key="0x073" accel-mods="4"><action>. '$APPDIR'/func -save</action></menuitem>
 <menuitem stock="gtk-save-as" label="'$(gettext 'Export tracks in queue')'..."><action>'$APPDIR'/func -export_playqueue</action></menuitem>
 <menuitem stock="gtk-cdrom" label="'$(gettext 'Burn Audio-CD of queue')'...">'
  if [ ! -f /usr/local/pburn/pburn ] || [ "$CD_DEVICE" = none ]; then S=$S'<visible>disabled</visible>'; fi
  S=$S'<action>'$APPDIR'/func -burn &</action>
 </menuitem>

 <menuitemseparator></menuitemseparator>

 <menu label="'$(gettext 'Sort')'">
  <menuitem label="'$(gettext 'By name')'">
   <action>cat '$WORKDIR'/playlist | sort -t"|" -k3,3 > '$WORKDIR'/playlist1; mv -f '$WORKDIR'/playlist1 '$WORKDIR'/playlist</action>
   <action>refresh:PLAYLIST</action>
  </menuitem>
  <menuitem label="'$(gettext 'By length')'">
   <action>cat '$WORKDIR'/playlist | sort -t"|" -k2,2 > '$WORKDIR'/playlist1; mv -f '$WORKDIR'/playlist1 '$WORKDIR'/playlist</action>
   <action>refresh:PLAYLIST</action>
  </menuitem>
  <menuitemseparator></menuitemseparator>
  <menuitem label="'$(gettext 'Shuffle')'">
   <action>cat '$WORKDIR'/playlist | sort -R > '$WORKDIR'/playlist1; mv -f '$WORKDIR'/playlist1 '$WORKDIR'/playlist</action>
   <action>refresh:PLAYLIST</action>
  </menuitem>
 </menu>

 <menuitemseparator></menuitemseparator>

 <menuitem stock="gtk-pmusic_undo" label="'$(gettext 'Go back in queue history')'" accel-key="0x07a" accel-mods="4">
  <action>. '$APPDIR'/func -undo</action>
  <action>refresh:PLAYLIST</action>
 </menuitem>
 <menuitem stock="gtk-pmusic_redo" label="'$(gettext 'Go forward in queue history')'" accel-key="0x079" accel-mods="4">
  <action>. '$APPDIR'/func -redo</action>
  <action>refresh:PLAYLIST</action>
 </menuitem>

 <menuitemseparator></menuitemseparator>

 <menuitem checkbox="'$(<$WORKDIR/PLAYLIST_MULTIPLE)'" label="'$(gettext 'Multiple PlayQueues')'">
  <action>if true show:PLAYQUEUE_TABS</action>
  <action>if true show:PLAYQUEUE_TABS_2</action>
  <action>if true echo true > '$WORKDIR'/PLAYLIST_MULTIPLE</action>
  <action>if false hide:PLAYQUEUE_TABS</action>
  <action>if false hide:PLAYQUEUE_TABS_2</action>
  <action>if false echo false > '$WORKDIR'/PLAYLIST_MULTIPLE</action>
  <action>rm -f '$WORKDIR'/xml_pmusic</action>'
  [[ "$*" = *skip_multiple_playqueues* ]] && S=$S'<sensitive>false</sensitive>'
 S=$S'</menuitem>
</menu>'
GUI_MENU_PLAYLIST="$S"




######################################################
#                                                    #
#                  P L A Y B A C K                   #
#                                                    #
######################################################

S='
<menu label="'$(gettext 'Playback')'">
 <menuitem label="'$(gettext 'Track info')'" stock="gtk-info" accel-key="0x069" accel-mods="4"><action>'$APPDIR'/func_trackinfo "" -gui &</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem stock="gtk-media-play" label="'$(gettext 'Play / Pause')'" accel-key="0x020" accel-mods="8"><action>'$APPDIR'/func_player -play -skip_update; echo true > '$WORKDIR'/UPDATE_GUI</action></menuitem>
 <menuitem stock="gtk-media-stop" label="'$(gettext 'Stop')'" accel-key="0xff54" accel-mods="8"><action>'$APPDIR'/func_player -stop -skip_update; cp '$APPDIR'/themes/'$THEME'/icon_play.svg '$WORKDIR'/icon_playpause.svg; echo true > '$WORKDIR'/UPDATE_GUI</action></menuitem>
 <menuitem stock="gtk-media-next" label="'$(gettext 'Next track')'" accel-key="0xff53" accel-mods="8"><action>cp -f '$WORKDIR'/PLAYLIST '$WORKDIR'/tmpPLAYLIST</action><action>'$APPDIR'/func_player -next -user_action</action></menuitem>
 <menuitem stock="gtk-media-previous" label="'$(gettext 'Previous track')'" accel-key="0xff51" accel-mods="8"><action>'$APPDIR'/func_player -back</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem label="'$(gettext 'Jump to time')'..."><action>'$APPDIR'/func -jump_to_time &</action></menuitem>
 <menuitem stock="gtk-timestamp" label="'$(gettext 'Add timestamp to favorites')'"><action>'$APPDIR'/func_favorites -add_favorites_with_timestamp &</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem stock="gtk-volume" label="'$(gettext 'Sound processing')'..."><action>'$APPDIR'/func_mixer -gui &</action></menuitem>
</menu>'
GUI_MENU_TRACK="$S"


S='
<menu label="'$(gettext 'Tools')'">
 <menu label="'$(gettext 'Plugins')'">
  <menuitem label="'$(gettext 'Plugin manager')'..."><action>'$APPDIR'/func -plugins &</action></menuitem>
  <menuitem label="'$(gettext 'Download plugins')'..."><action>export GTK2_RC_FILES='$HOME'/.gtkrc-2.0; $BROWSER "$PLUGIN_URL" &</action></menuitem>
  <menuitemseparator></menuitemseparator>'
	find $APPDIR/plugins/ -name "*.rc" > $WORKDIR/tmp_gui_menu
	while read I; do
		if [ "`grep -F 'PLUGIN_RUN_AT_START=false' "$I"`" ]; then
			TMP="`grep -F 'PLUGIN_EXEC' "$I" | cut -d= -f2 | cut -d'"' -f2 | cut -d'"' -f1`"
			TMP1="`grep -F 'PLUGIN_NAME' "$I" | cut -d= -f2 | cut -d'"' -f2 | cut -d'"' -f1`"
			TMP2="`grep -F 'PLUGIN_IMAGE' "$I" | cut -d= -f2 | cut -d'"' -f2 | cut -d'"' -f1`"
			S=$S'<menuitem image-name="'$TMP2'" label="'$TMP1'..."><height>28</height><action>'$APPDIR'/plugins/'$TMP' &</action></menuitem>'
		fi
	done < $WORKDIR/tmp_gui_menu
  S=$S'
 </menu>
 <menuitemseparator></menuitemseparator>
 <menuitem label="'$(gettext 'Mass tagger')'..."><action>'$APPDIR'/func_id3tagger '$WORKDIR'/playlist &</action></menuitem>
 <menuitem label="'$(gettext 'Radio grabber')'..."><action>. '$APPDIR'/func_radiohits &</action></menuitem>
</menu>'
GUI_MENU_TOOLS="$S"


S='
<menu label="'$(gettext 'View')'">
 <menuitem stock="gtk-fullscreen" label="'$(gettext 'Now playing')'">
  <action>exec pmusic -j -p .NowPlaying -T .NowPlaying</action>
 </menuitem>
 <menuitemseparator></menuitemseparator>
 <menu label="'$(gettext 'Docks')'">
  <menuitem stock="gtk-find" label="'$(gettext 'Show/hide Search options')'">
   <action type="activate">VIEW_SEARCH_OPTIONS</action>
  </menuitem>
  <menuitem stock="gtk-pmusic_add" label="'$(gettext 'Show/hide Smart Add')'">
   <action type="activate">VIEW_ADD_OPTIONS</action>
  </menuitem>
 </menu>
 <menu label="'$(gettext 'Frontends')'">'
	while read I; do
		S=$S'
		<menuitem label="'$I'" stock="gtk-pmusic">
		 <action>pmusic -j -p "'$I'"</action>
		</menuitem>'
	done <<< "$(find $APPDIR/frontends/ -mindepth 1 -printf "%f\n" | grep -vE "${PMUSIC_FRONTEND##*/}|.NowPlaying")"
 S=$S'</menu>
 <menu label="'$(gettext 'Visualization')'">'
	if [ $VIDEO_SERVER = false ]; then
		S=$S'
		<menuitem label="'$(gettext 'Activate Visualization')'...">
			<action>'$APPDIR'/func_config -preferences -video</action>
		</menuitem>'
		VIDEO_SENSITIVE='<sensitive>false</sensitive>'
	fi
  S=$S'<menuitem radiobutton="false" label="'$(gettext 'None')'">
   '$VIDEO_SENSITIVE'
   <action>if true rm '$WORKDIR'/visualization-*; echo > '$WORKDIR'/visualization_cmd</action>
   <action>if true killall ffplay</action>
  </menuitem>'
	while read I; do
		IFS='|' read NAME CMD <<< "$I"
		S=$S'
		<menuitem radiobutton="false" label="'$NAME'">
		 <input file>"'$WORKDIR'/visualization-'$NAME'"</input>
		 '$VIDEO_SENSITIVE'
		 <action>if true rm '$WORKDIR'/visualization-*; echo true > "'$WORKDIR'/visualization-'${NAME}'"</action>
		 <action>if true '$APPDIR'/func -visualization "'$NAME'" &</action>
		</menuitem>'
	done < $APPDIR/txt_visualization
  S=$S'
  <menuitemseparator></menuitemseparator>
  <menuitem checkbox="false" label="'$(gettext 'Fullscreen')'  ( f )">
   <input file>'$WORKDIR'/visualization_fullscreen</input>
   '$VIDEO_SENSITIVE'
   <action>if true echo true > '$WORKDIR'/visualization_fullscreen</action>
   <action>if false echo false > '$WORKDIR'/visualization_fullscreen</action>
   <action>'$APPDIR'/func -visualization &</action>
  </menuitem>
 </menu>
</menu>'
GUI_MENU_VIEW="$S"


GUI_MENU_HELP='
<menu label="'$(gettext 'Help')'">
 <menuitem stock="gtk-about" label="'$(gettext 'About')'"><action>'$APPDIR'/func -about &</action></menuitem>
 <menuitem stock="gtk-help" label="'$(gettext 'Help')'" accel-key="0xffbe" accel-mods="0"><action>'$APPDIR'/func -help</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem label="'$(gettext 'Dependency check')'"><action>'$APPDIR'/func -dependency_check</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem stock="gtk-help" label="'$(gettext 'I want music')'"><action>'$APPDIR'/func -info_IWantMusic</action></menuitem>
</menu>'

S='
<hbox spacing="1">
 <hbox '$ST'>
 <menubar name="menu_main">
  '$GUI_MENU_FILE'
  '$GUI_MENU_SOURCES'
  '$GUI_MENU_PLAYLIST'
  '$GUI_MENU_TRACK'
  '$GUI_MENU_VIEW'
  '$GUI_MENU_TOOLS'
  '$GUI_MENU_HELP'
  <action>rm -f $WORKDIR/history</action> ###reset undo/redo function
 </menubar>
 </hbox>'
 if [ $FIRST_RUN = true ]; then S=$S'
 <button>
  <input file stock="gtk-help"></input>
  <label>'$(gettext 'I want music')'</label>
  <action>'$APPDIR'/func -info_IWantMusic</action>
 </button>'
 fi
GUI_MENUS=$S'</hbox>'

